package org.test.client.ui.launch;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Stroke;

import javax.swing.JPanel;
import javax.swing.JWindow;

import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

/**
 * 装载 mainframe 的 进度窗
 * 
 * @since 20150717
 * @author Guodeqi
 *
 */
public class ProgressWindow extends JWindow{
	ProgressWindow thisw = this;
	LoadingPanel loadingPanel = null;
	int w = 200;
	int h = 150;
	
	public ProgressWindow(){
		loadingPanel = new LoadingPanel(w,h,false);
		loadingPanel.setStop(false);
//		loadingPanel = new LoadingPanel(w,h,true);
		
		UIUtil.setWindowOpaque(this, false);
		this.getContentPane().add(loadingPanel);
		this.setSize(w, h);
		UIUtil.centerWindow(this);
		this.setAlwaysOnTop(true);
		
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					
					for (int i = 1; i < 101; i++) {
						loadingPanel.progress = i;
						loadingPanel.repaint();
						Thread.sleep(20);
					}
					loadingPanel.setFinish();
					Thread.sleep(1000);
//					thisw.dispose();
					
//					//start
//					loadingPanel.setStop(false);
//					Thread.sleep(2000);
//					//stop
//					loadingPanel.setStop(true);
//					Thread.sleep(2000);
//					//start
//					loadingPanel.setStop(false);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
//		t.start();
		
	}
	
	public static void main(String[] args) {
		ProgressWindow w = new ProgressWindow();
		w.setVisible(true);
	}
	
	
}

class LoadingPanel extends JPanel implements Runnable{
	
	Color back1 = Icons.getColor("ffffff");
	Color back2 = Icons.getColor("f7f7f7");

	Color borderNormal = Icons.getColor("cccccc");
	Color borderFill = Icons.getColor("66afe9");
	Color fontColor = Color.black;
	
	public boolean isFinish = false;
	String loadingStr = "Loading...";
	String finishStr = "OK.";
	//字与 圆圈的 间隙
	int vgap = 10;
	//圆的直径
	int roundDiameter = 80;
	int roundBorderw = 1;
	
	int rectDiameter = 24;
	
	int w = 0;
	int h = 0;
	//	是否需要具体进度
	boolean needProgress = true;
	
	//线程是否在运行
	boolean threadRun = true;
	boolean waitFlag = true;
	
	public int progress = 0;
	//若没有具体进度则 用这两个
	int rotateAngle = 0;
	int borderFillAngel = 90;
	
	public Thread thisRun = null;
	
	public LoadingPanel(int w, int h, boolean needProgress) {
		this.w = w;
		this.h = h;
		this.needProgress = needProgress;
		this.setOpaque(false);
		this.setPreferredSize(new Dimension(w,h));
		thisRun = new Thread(this);
		if(!needProgress && !waitFlag){
			thisRun.start();
		}
	}

	public LoadingPanel(int w,int h){
		this(w,h,true);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
//		Graphics2D g2d = (Graphics2D) g2.create();
		UIUtil.setAntiAliasing(g2d, true);
		int www = this.getWidth();
		int hhh = this.getHeight();
		
		AlphaComposite srcOverAC = UIUtil.getSrcOverAC(0.8f);
		Composite oldComposite = g2d.getComposite();
		g2d.setComposite(srcOverAC);
		Paint paint = UIUtil.getPaint(back1, back2, 0, 0, 0, hhh);
		g2d.setPaint(paint);
		g2d.fillRoundRect(0, 0, www, hhh, rectDiameter, rectDiameter);
		
		g2d.setComposite(oldComposite);
		//draw indicator
		Stroke stroke = UIUtil.getStroke(roundBorderw);
		g2d.setStroke(stroke);
		g2d.setColor(borderNormal);
		int rx = www/2 - roundDiameter/2;
		int ry = hhh/2  - (UIUtil.getTxtHeight(g2d)+vgap +roundDiameter)/2;
		g2d.drawOval(rx, ry, roundDiameter, roundDiameter);
		
		g2d.setFont(new Font(Font.DIALOG,Font.PLAIN,14));
		
		//draw fill border
		Stroke stroke2 = UIUtil.getStroke(roundBorderw);
		g2d.setStroke(stroke2);
		g2d.setColor(borderFill);
		if(needProgress){
			int tmpAngle = progress*360/100;
			g2d.drawArc(rx, ry, roundDiameter, roundDiameter, 90-tmpAngle, tmpAngle);
			drawHighLight(rx, ry, roundDiameter, 90-tmpAngle, tmpAngle,g2d);
			
			//draw progress  demo:10% .. 11%
			g2d.setColor(fontColor);
			String progressStr = progress+"%";
			int px = www/2 - UIUtil.getTxtWidth(g2d,progressStr)/2;
			int py = ry + roundDiameter/2+ UIUtil.getTxty(g2d);
			g2d.drawString(progressStr, px, py);
		}else{
			g2d.drawArc(rx, ry, roundDiameter, roundDiameter, (360-rotateAngle), borderFillAngel);
			drawHighLight(rx, ry, roundDiameter, (360-rotateAngle), borderFillAngel,g2d);
		}
		
		String tmpStr = loadingStr;
		if(isFinish){
			tmpStr = finishStr;
		}
		int x = www/2 - UIUtil.getTxtWidth(g2d, tmpStr)/2;
		int y = ry + roundDiameter + vgap + g2d.getFontMetrics().getAscent();
		g2d.setColor(fontColor);
		g2d.drawString(tmpStr, x, y);
		
		
	}

	private void drawHighLight(int x, int y, int diameter, int sangle, int angle, Graphics2D g2d) {
		AlphaComposite c1 = UIUtil.getSrcOverAC(0.4f);
		AlphaComposite c2 = UIUtil.getSrcOverAC(0.2f);
		
		Composite tmp = g2d.getComposite();
		g2d.setComposite(c1);
		x = x +1;
		y = y +1;
		diameter = diameter-2;
		g2d.drawArc(x, y, diameter, diameter, sangle, angle);
		
		x = x -2;
		y = y -2 ;
		diameter = diameter + 4;
		g2d.drawArc(x, y, diameter, diameter, sangle, angle);
		
		g2d.setComposite(c2);
		x = x -1;
		y = y - 1;
		diameter = diameter+2;
		g2d.drawArc(x, y, diameter, diameter, sangle, angle);
		
		x = x + 4;
		y = y + 4;
		diameter = diameter - 8;
		g2d.drawArc(x, y, diameter, diameter, sangle, angle);
		
		g2d.setComposite(tmp);
	}

	@Override
	protected void paintBorder(Graphics g) {
	}
	
	@Override
	public void run() {
		try {
			while(threadRun){
				updateRotate();
				int sleep = 20;
				Thread.sleep(sleep);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateRotate() throws InterruptedException{
		synchronized (this) {
			if(waitFlag){
				this.wait();
			}
			if(rotateAngle < 180){
				rotateAngle =  rotateAngle+5;
			}else{
				rotateAngle =  rotateAngle+5;
			}
			if(rotateAngle > 360){
				rotateAngle = 0;
			}
			this.repaint();
		}
	}
	private boolean isFirstRun = true;
	//设置暂停
	public void setStop(boolean flag){
		synchronized (this) {
			this.waitFlag = flag;
			if( !flag ){
				if(isFirstRun){
					thisRun.start();
					isFirstRun = false;
				}else{
					this.notifyAll();
				}
			}
		}
	}
	
	public void setFinish(){
		this.isFinish = true;
		if(!needProgress){
			threadRun = false;
		}
		this.repaint();
	}
}



